<?php

namespace app\models;

use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
/**
 * This is the model class for table "{{%wm_product}}".
 *
 * @property integer $id
 * @property integer $uid
 * @property string $product_name
 * @property string $package_name
 * @property integer $created_at
 */
class WmProduct extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => 'created_at',
//                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
                ]
                //'value' => function() { return date('U'); // unix timestamp },
            ]
        ];
    }
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%wm_product}}';
    }

    public function getActiveCount()
    {
        // 关联的依据是 Order.customer_id = Customer.id
//        return $this->hasMany(WmActive::className(), ['pid' => 'id'])->count();
        return $this->hasMany(WmActive::className(), ['pid' => 'id'])->sum('num');
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['uid', 'created_at'], 'integer'],
            [['package_name'], 'required'],
            [['product_name', 'package_name'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'uid' => Yii::t('app', 'Uid'),
            'activeCount' => Yii::t('app', 'Active Count'),
            'product_name' => Yii::t('app', 'Product Name'),
            'package_name' => Yii::t('app', 'Package Name'),
            'created_at' => Yii::t('app', 'Created At'),
        ];
    }
}
